package com.bestcem.xm.common.core.domain.web;

import com.bestcem.xm.common.core.enums.CommonErrorEnum;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

/**
 * service 层返回结果封装类
 * 最后更新时间: 2021年6月4日15:05:55
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021-05-08
 */
@Setter
@Getter
@ToString
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ServiceResult<T> {

    private T data;

    private String msg;

    private String errorCode = "";

    private boolean success;

    /**
     * 错误信息
     */
    private CommonErrorEnum errorData;

    public static <T> ServiceResult<T> success() {
        return success(null, "");
    }

    public static <T> ServiceResult<T> success(T data) {
        return success(data, "");
    }

    public static <T> ServiceResult<T> success(T data, String msg) {
        ServiceResult<T> serviceResult = new ServiceResult<>();
        serviceResult.setSuccess(Boolean.TRUE);
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        return serviceResult;
    }

    public static <T> ServiceResult<T> fail(String msg) {
        return fail(null, msg);
    }

    public static <T> ServiceResult<T> failWithCode(CommonErrorEnum errorData) {
        ServiceResult<T> serviceResult = fail(null, errorData.getMsg());
        serviceResult.setErrorData(errorData);
        return serviceResult;
    }

    public static <T> ServiceResult<T> failWithCode(CommonErrorEnum errorData, String msg) {
        ServiceResult<T> serviceResult = fail(null, msg);
        serviceResult.setErrorData(errorData);
        return serviceResult;
    }

    public static <T> ServiceResult<T> fail(T data, String msg) {
        ServiceResult<T> serviceResult = new ServiceResult<>();
        serviceResult.setSuccess(Boolean.FALSE);
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        serviceResult.setErrorCode("500");
        return serviceResult;
    }

    public static <T> ServiceResult<T> fail(T data, String msg, String errorCode) {
        ServiceResult<T> serviceResult = new ServiceResult<>();
        serviceResult.setSuccess(Boolean.FALSE);
        serviceResult.setData(data);
        serviceResult.setMsg(msg);
        serviceResult.setErrorCode(errorCode);
        return serviceResult;
    }

    public ServiceResult<T> setErrorCode(String errorCode) {
        this.errorCode = errorCode;
        return this;
    }

    public static <T> ServiceResult<T> fail(CommonErrorEnum errorEnum) {
        return fail(errorEnum, null);
    }

    /**
     * 构造失败返回参数
     *
     * @param errorEnum 错误码信息
     * @param msg       错误描述信息
     * @param <T>
     * @return
     */
    public static <T> ServiceResult<T> fail(CommonErrorEnum errorEnum, String msg) {
        ServiceResult<T> serviceResult = new ServiceResult<>();
        serviceResult.setSuccess(Boolean.FALSE);
        if (errorEnum == null) {
            serviceResult.setErrorCode("500");
            serviceResult.setMsg("UNKNOW");
        } else {
            serviceResult.setErrorCode(String.valueOf(errorEnum.getCode()));
            serviceResult.setMsg(errorEnum.getMsg());
        }
        serviceResult.setErrorData(errorEnum);
        if (StringUtils.isNotEmpty(msg)) {
            serviceResult.setMsg(msg);
        }
        return serviceResult;
    }
}

